# Copyright 2021 Tianmian Tech. All Rights Reserved.
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Copyright 2019 The FATE Authors. All Rights Reserved.
# 
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#     http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from common.python.utils import log_utils

LOGGER = log_utils.get_logger()


def assert_io_num_rows_equal(func):
    def _func(*args, **kwargs):
        input_count = None
        all_args = []
        all_args.extend(args)
        all_args.extend(kwargs.values())
        for arg in all_args:
            if type(arg).__name__ in ["DSource", "RDDSource"]:
                input_count = arg.count()
                break

        result = func(*args, **kwargs)

        if input_count is not None and type(result).__name__ in ["DSource", "RDDSource"]:
            output_count = result.count()
            LOGGER.debug(f"num row of input: {input_count} -> num row of output: {output_count}")
            if input_count != output_count:
                raise EnvironmentError(
                    f"num row of input({input_count}) not equals to num row of output({output_count})")
        return result

    return _func
